/**
 *
 *		File: ajax_function.js
 *		Author: Sasa Milojevic
 *		E-mail: milojevic.sasa@gmail.com
 *		Date: 2012-02-20
 *
 */

function update_num_online()
{
    var ajaxURL = AJAX_URL + 'update_num_online/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        // data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';

            if(parseInt(parsed.ind) == 1)
            {
                $('#num_online').html(parsed.num_online);  
                //if( typeof parsed.num_online_users != "undefined" )
                $('#num_users_online').html(parsed.num_users_online);                
                $('#num_moderators').html(parsed.num_moderators);
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita 
}

// Stalno pozivanje f-je da updateuje broj online korisnika
setInterval(update_num_online, REFRESH_TIME); // svake 5 sekunde


function update_online_users()
{
    var ajaxURL = AJAX_URL + 'update_online_users/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        // data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            $('#table_users_online tbody').html(''); 
            if(parseInt(parsed.ind) == 1)
            {
                //$('#num_online').html(parsed.num_online); 
                // Ajax refresh tabele sa podacima
                $('#table_users_online tbody').html(parsed.html);  
                $("#table_users_online").trigger("update");
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita 
}




function filter_submit()
{
    var ajaxURL = CONTROLLER_URL + 'filter_submit/';
    var data = $("#form-filter").serialize();
    
    $('table tbody').html("");  
    $("table").trigger("update");
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            //$('table tbody').html(''); 
            //$("table").trigger("update");
            if(parseInt(parsed.ind) == 1)
            {
                $('#stats').html(parsed.num_users); 
                // Ajax refresh tabele sa podacima
                $('table tbody').html(parsed.html);  
                $("table").trigger("update");
                               
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita 
}

function fake_users_filter_submit()
{
    var ajaxURL = CONTROLLER_URL + 'fake_users_filter_submit/';
    var data = $("#form-filter").serialize();
    
    $('table tbody').html("");  
    $("table").trigger("update");
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            //$('table tbody').html(''); 
            //$("table").trigger("update");
            if(parseInt(parsed.ind) == 1)
            {
                $('#stats').html(parsed.num_users); 
                // Ajax refresh tabele sa podacima
                $('table tbody').html(parsed.html);  
                $("table").trigger("update");
                               
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita 
}

function view_user_activity(id)
{
    var ajaxURL = BASE_URL + 'administration/users/get_user_activity/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            $('#dialog-user').html(''); 
            if(parseInt(parsed.ind) == 1)
            {               
                $('#dialog-user').html(parsed.html);  
                $("#dialog-user").dialog("open");
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita   
}


function save_user()
{
    var ajaxURL = CONTROLLER_URL + 'save_user/';
    var data = $('#form-user-add-edit').serialize();
    var is_fake_list = $('#is_fake_list').val();
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data+"&is_fake_list="+is_fake_list,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            
            if(parseInt(parsed.ind) == 1)
            {               
                $('#dialog-user-add-edit').dialog('close'); // close form
                $('#stats').html(parsed.num_users);  // update num user
                // Ajax refresh tabele sa podacima
                $('table tbody').html(parsed.html); 
                $("table").trigger("update");
                $('#form-filter')[0].reset();
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita   
}

/**
 *  Menja status stavke active= true/false
 *  id - id stavke
 *  table - naziv tabale
 */
function change_active(id, table)
{
    var ajaxURL = AJAX_URL + 'change_active/';
    // Postavi kursor u wait stanje
    $("body").css("cursor", "wait");
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'id='+id
        +'&table='+table,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';

            if(parseInt(parsed.ind) == 1)
            {
                // Formiranje poruke
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>"; 
                html += parsed.msg +"</p>";
                // Priakz poruke
                $('#response').show(500).addClass('ui-state-highlight ui-corner-all').html(html).delay(1500).hide(500);
                // Ajax refresh tabele sa podacima
                $('#data-table tbody').html(parsed.html);               
                
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }
            // Vrati kursor na normal
            $("body").css("cursor", "auto");
        }  
    }); // end AJAX upita 
}


function select_user(id)
{
    //alert("User. ID="+id);
    $('tr').removeClass("tr_selected");
    $('#user_row_'+id).addClass("tr_selected");
    $('#selected_user_id').val(id);
}

function edit_user()
{
    var id = $('#selected_user_id').val();
    
    var ajaxURL = CONTROLLER_URL + 'get_user_data/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {               
                $("#user_id").val(parsed.id);
                $("#user_ip_address").val(parsed.ip_address);
                $("#user_state").val(parsed.state);
                $("#user_username").val(parsed.username);
                $("#user_status").attr('disabled',false);
                $("#user_status").val(parsed.status);                
                $("#user_password").attr('disabled',false);
                $("#user_zip_code").val(parsed.zip_code);
                $("#user_password").val('');
                //$("#user_password").attr('readonly',true);
                $("#user_gender").val(parsed.gender);
                $("#user_affiliate").val(parsed.affiliate);
                $("#user_email").val(parsed.email);
                $("#user_look_for").val(parsed.look_for);
                $("#user_registration_date").val(parsed.registration_date);
                $("#user_birthdate").val(parsed.birthdate);
                $("#user_user_group").val(parsed.user_group);
                $("#user_team").val(parsed.team_id);
                $("#user_flirt_text").val(parsed.flirt_text);
                if( 1 == parsed.is_fake )
                {
                  $("#user_is_fake").attr('checked',true);                 
                }
                else
                {
                  $("#user_is_fake").attr('checked',false);
                }
                $("#add_to_spotlight").attr('disabled',false);
                
                
                $("#dialog-user-add-edit").dialog("open");
            }
            else
            {
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
            }            
        }  
    }); // end AJAX upita  
}


function add_to_spotlight()
{
  var id = $('#selected_user_id').val();
  
  var ajaxURL = CONTROLLER_URL + 'add_to_spotlight/';
    $('#user_response').hide();
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';            
            if(parseInt(parsed.ind) == 1)
            {                
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#user_response').show().addClass('ui-state-info ui-corner-all').html(html).delay(3000).hide(500);
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-error'></span>";      
                html += parsed.msg +"</p>";

                $('#user_response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
            }            
        }  
    }); // end AJAX upita 
  
}

/**
 *  Populate user mail dialog
 */
function mailto_user()
{
    var id = $('#selected_user_id').val();
    
    var ajaxURL = CONTROLLER_URL + 'get_user_data/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {               
                $("#mailto_id").val(parsed.id);
                $("#mailto_email").val(parsed.email);
                $("#mailto_username").val(parsed.username); 
                
                $("#mailto_birthdate").html(parsed.birthdate);
                $("#mailto_state").html(parsed.state_name);
                $("#mailto_gender").html(parsed.gender_text);
                $("#mailto_look_for").html(parsed.look_for_text);
                $("#mailto_username_text").html(parsed.username);               
                $("#mailto_zip_code").html(parsed.zip_code);
                
                $("#dialog-mailto-user").dialog("open");
            }
            else
            {
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
            }            
        }  
    }); // end AJAX upita 
}

/**
 *  Send mail to user
 */
function send_user_mail()
{    
    $('#mail_response').removeClass('ui-state-highlight ui-state-error ui-corner-all');
    $('#response').removeClass('ui-state-highlight ui-state-error ui-corner-all');
    $("body").css("cursor", "wait");
    var data = $("#form-mailto-user").serialize();    
    var ajaxURL = CONTROLLER_URL + 'send_user_mail/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {               
                $("#dialog-mailto-user").dialog("close");
                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";  
                html += parsed.msg +"</p>";
                
                $('#response').show(500).addClass('ui-state-highlight ui-corner-all').html(html).delay(2000).hide(500);
            }
            else
            {
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#mail_response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
            }     
            $("body").css("cursor", "auto");
        }  
    }); // end AJAX upita 
    
    
    
    
   
}

function delete_image( field_name, upload_dir , file )
{
    $('#response').removeClass('ui-state-highlight ui-state-error ui-corner-all');
    var ajaxURL = AJAX_URL + 'delete_image/';
    $("body").css("cursor", "wait");
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'upload_dir='+upload_dir+"&file="+file,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';

            if(parseInt(parsed.ind) == 1)
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";  
                html += parsed.msg +"</p>";

                $('#response').show(500).addClass('ui-state-highlight ui-corner-all').html(html).delay(1500).hide(500);
                          
                $( "#div_"+field_name ).remove(); // izbrisi prikaz naziva slike i linka za brisanje
                $("#upload_"+field_name).attr('disabled',false); // omoguci Upload
                $("#"+field_name).val(''); // Obrisi value u hidden polju
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>"; 
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html).delay(2000).hide(500); 
            }
            $("body").css("cursor", "auto");
        }  

    }); // end AJAX upita 
}


function change_lang( new_lang )
{
    var ajaxURL = AJAX_URL + 'change_lang/';
    $("body").css("cursor", "wait");
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'lang='+new_lang,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';

            if(parseInt(parsed.ind) == 1)
            {
                location.reload();
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>"; 
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html).delay(2000).hide(500); 
            }
            $("body").css("cursor", "auto");
        }  

    }); // end AJAX upita 
}

function update_to_users( id ) {
    var ajaxURL = AJAX_URL + 'update_to_users/';
        
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'from='+id,
        cache: false,
        async: false,
        success: function(response){
          
            var parsed = JSON.parse(response);
          
            var options = '';
            if( parsed.to_users != null )
                for (var i = 0; i < parsed.to_users.length; i++) {
          
                    options += '<option value="' + parsed.to_users[i].id + '">' + parsed.to_users[i].username + '</option>';
                } 
            $("#sb_to").html(options);
        }  

    }); // end AJAX upita 
}

function update_monitor()
{
    var ajaxURL = AJAX_URL + 'update_monitor/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            $('#monitor tbody').html(''); 
            if(parseInt(parsed.ind) == 1)
            {
                
                // Ajax refresh tabele sa podacima
                
                $('#monitor tbody').html(parsed.html);  
                $("#monitor").trigger("update");
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita 
}



function selected_submit(){
    var ajaxURL = CONTROLLER_URL + 'selected_submit/';
    var data = $("#form-select").serialize();
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {                
                $("#dialog-mailto-users").dialog("open");
                $("#dialog-promotion-code").dialog("open");
            }
            else
            {
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
            }                        
        }
    }); // end AJAX upita 
}


/**
 *  Send mail to user
 */
function send_newsletter()
{    
    var emails = $("#form-select").serialize();
    var subject = $("#mailto_subject").val();
    var message = $("#mailto_message").val();
    var ajaxURL = CONTROLLER_URL + 'send_newsletter/';
    $("#mail_response").html("please wait...");
    $("#mailto_newsletter").attr("disabled",true);
    $("body").css("cursor", "wait");
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: emails + "&subject="+subject+"&message="+message,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {         
                $("#mail_response").html('');
                $("#dialog-mailto-users").dialog("close");
                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";  
                html += parsed.msg +"</p>";
                
                $('#response').show(500).addClass('ui-state-highlight ui-corner-all').html(html).delay(2000).hide(500);
                //$("#dialog-mailto-users").dialog("close");
            }
            else
            {                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#mail_response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
                
            }     
            $("body").css("cursor", "auto");
            $("#mailto_newsletter").attr("disabled",false);
        }  
    }); // end AJAX upita 
     return false;  
}

function send_promotion_code()
{    
    var emails = $("#form-select").serialize();
    //var subject = $("#mailto_subject").val();
    var message = $("#mailto_message").val();
    var mailto_form = $("#form-promotion-code").serialize();
    
    var ajaxURL = CONTROLLER_URL + 'send_promo_code/';
    var data = emails + "&" + mailto_form
    //alert(data);
    //return;
    
    $("#mail_response").html("please wait...");
    $("#send_promotion_code").attr("disabled",true);
    $("body").css("cursor", "wait");
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data+"&message="+message,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {         
                $("#mail_response").html('');
                $("#dialog-mailto-users").dialog("close");
                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";  
                html += parsed.msg +"</p>";
                
                $('#mail_response').show(500).addClass('ui-state-highlight ui-corner-all').html(html).delay(2000).hide(500);
                //$("#dialog-mailto-users").dialog("close");
            }
            else
            {                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#mail_response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
                
            }     
            $("body").css("cursor", "auto");
            $("#send_promotion_code").attr("disabled",false);
        }  
    }); // end AJAX upita 
     return false;  
}

function showSupportMessages(userId){
     var ajaxURL = CONTROLLER_URL + 'show_support_messages/';
     var data = userId;
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: {userId:data},
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
            $('#support_messages tbody').html(''); 
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
                $('#support_messages tbody').html(parsed.html);  
                $("#support_messages").trigger("update");
            }
            else
            {
                var html = '';
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
    }); // end AJAX upita 
}
function refreshUsers(){
    var ajaxURL = CONTROLLER_URL + 'refresh_users/';
    // Ajax upit
    $.ajax({  
        type: "GET", 
        url: ajaxURL,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
            $('#support_users').html(''); 
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
                $('#support_users').html(parsed.html);  
                $("#support_users").trigger("update");
            }
            else
            {
                var html = '';
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
    }); // end AJAX upita 
}
function submit_support_message(userId){
    var ajaxURL = CONTROLLER_URL + 'submit_support_message/';
    var message = $("#support_message").val();
    $("#submit_support_message").attr("disabled",true);
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: "userId="+userId+"&message="+message,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {         
                $("#support_response").html('');
                $("#support_message").val("");
                $('#support_messages tbody').html(parsed.html);  
                $("#support_messages").trigger("update");
            }
            else
            {                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";
            }     
            $("body").css("cursor", "auto");
            $("#submit_support_message").attr("disabled",false);
        }  
    }); // end AJAX upita 
     return false;  
}
function change_billing_period(period){
    var ajaxURL = CONTROLLER_URL + 'change_billing_period/';
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: "period="+period,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {         
                $("#total").html('');
                $("#total").html(parsed.amount);
              
           }
            else
            {                
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";
            }     
         
        }  
    }); // end AJAX upita 
     return false;  
}


function select_team(id)
{
    //alert("User. ID="+id);
    $('tr').removeClass("tr_selected");
    $('#team_row_'+id).addClass("tr_selected");
    $('#selected_team_id').val(id);
}

function edit_team()
{
    var id = $('#selected_team_id').val();
    
    var ajaxURL = CONTROLLER_URL + 'get_team_data/';
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: 'id='+id,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {               
                $("#team_id").val(parsed.id);
                $("#team_name").val(parsed.name);
                $("#team_description").val(parsed.description);
               
                $("#dialog-team").dialog("open");
            }
            else
            {
                var html = "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html).delay(3000).hide(500); 
            }            
        }  
    }); // end AJAX upita  
}

function save_team()
{
    var ajaxURL = CONTROLLER_URL + 'save_team/';
    var data = $('#form-team').serialize();
    
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: data,
        cache: false,
        async: false,
        success: function(response){

            var parsed = JSON.parse(response);
            var html = '';
            
            if(parseInt(parsed.ind) == 1)
            {               
                $('#dialog-team').dialog('close'); // close form                
                // Ajax refresh tabele sa podacima
                $('#data-table tbody').html(parsed.html); 
                $("table").trigger("update");
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }            
        }  
    }); // end AJAX upita   
}
function getConversation(fake, user){
    var userFrom=user.toString();
    var userTo=fake.toString();
    var tabs = $( "#tabs" ).tabs();
    var ul = tabs.find( "ul" );
    var unFrom='usernameFrom_'+userFrom;
    var unTo='usernameTo_'+userTo;
    var usernameFrom=$("#"+unFrom).val();
    var usernameTo=$("#"+unTo).val();
    var id=userFrom+userTo;
    var table_id ='table_' +id;
    var table =tabs.find( "#"+table_id );
    
    if(table.val()==null){
    $( "<li><a href='#"+table_id+"' id='tab_"+table_id+"' name='tab_a'>"+usernameFrom+" <=> "+usernameTo+"</a><span class='ui-icon ui-icon-close' style='float:right'; role='presentation'>Remove Tab</span></li>" ).appendTo( ul );
    $( "<table id='"+table_id+"' class='dialog_table right_col' width='100%' height='100%' cellpadding=3px cellspacing=0><tbody ></tbody></table>" ).appendTo( tabs );
    tabs.tabs( "refresh" );
    var ajaxURL = CONTROLLER_URL + 'get_Conversation/';
     // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: "userFrom="+userFrom+"&userTo="+userTo,
        cache: false,
        async: false,
        success: function(response){
           
            var parsed = JSON.parse(response);
            var html = '';
            
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
              
                $("#"+table_id+" tbody").html(parsed.html);  
                $("#"+table_id).trigger("update");
                $("#tab_"+table_id).trigger('click');
                $("#send_mod_message").removeAttr('disabled');
            }
            else
            {
                
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
                $("#send_mod_message").attr('disabled', 'disabled');
            }                              
        }
       
    }); // end AJAX upita 
   
    }
    else {
        $("#tab_"+table_id).click();
    }
    
    tabs.tabs( "refresh" );
}
function send_mod_message(userFrom, userTo){
    var table_id ='table_' +userTo+userFrom;
    var message = $("#mod_message").val();
    var ajaxURL = CONTROLLER_URL + '/send_mod_message/';
     // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: "userFrom="+userFrom+"&userTo="+userTo+"&message="+message,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
            var html = '';
            
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
               
                $("#"+table_id+" tbody").html(parsed.html);  
                $("#"+table_id).trigger("update");
                $("#tab_"+table_id).trigger('click');
                $("#mod_message").val('');
                
                $("#table_"+table_id).parent().scrollTop = $("#table_"+table_id).parent().scrollHeight;
            }
            else
            {
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
       
    }); // end AJAX upita 
}
function refresh_conversations(){
    
    var ajaxURL = CONTROLLER_URL + '/refresh_conversations/';
    // Ajax upit
    $.ajax({  
        type: "GET", 
        url: ajaxURL,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
            $('#conversation_list').html(''); 
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
                $('#conversation_list').html(parsed.html);  
                $("#conversation_list").trigger("update");
               // $(".conversation_div:first-child").addClass('selected');
           }
            else
            {
                var html = '';
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
    }); // end AJAX upita 
}
function update_conversation(){
    var userFrom=$(".selected").find("#conversation_fake").val();
    var userTo=$(".selected").find("#conversation_user").val();
    if ((typeof userFrom === "undefined") ||(typeof userTo === "undefined")){
       
    }else{
    
    var tabs = $( "#tabs" ).tabs();
    var id=userFrom+userTo;
    var table_id ='table_' +id;
    
    var ajaxURL = CONTROLLER_URL + 'get_Conversation/';
     // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: "userFrom="+userFrom+"&userTo="+userTo,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
           
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
               
                $("#"+table_id+" tbody").html(parsed.html);  
                $("#"+table_id).trigger("update");
                $("#tab_"+table_id).trigger('click');
            }
            else
            {
                var html = '';
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
       
    }); // end AJAX upita 
    
    }
}
function update_mod_activity(){
   
    var ajaxURL = AJAX_URL + 'update_mod_activity/';
     // Ajax upit
    $.ajax({  
        type: "GET", 
        url: ajaxURL,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
           
            if(parseInt(parsed.ind) == 1)
            {
               // Ajax refresh tabele sa podacima
              
            }
            else
            {
                var html = '';
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
       
    }); // end AJAX upita 
    
    
}
function start_conversation(){
     var ajaxURL = CONTROLLER_URL + 'start_conversation/';
     $("#dialog-start-conversation").html('');
     // Ajax upit
    $.ajax({  
        type: "GET", 
        url: ajaxURL,
        cache: false,
        async: false,
        success: function(response){
            
            var parsed = JSON.parse(response);
           
            if(parseInt(parsed.ind) == 1)
            {
               $("#dialog-start-conversation").html(parsed.html);
               $("#dialog-start-conversation").dialog("open");
              
            }
            else
            {
                var html = '';
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }                              
        }
       
    }); // end AJAX upita 
}
function submit_conversation(){
    var conversation = $("#form-start-conversation").serialize();
    var ajaxURL = CONTROLLER_URL + '/submit_conversation/';
   
    $("body").css("cursor", "wait");
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: conversation,
        cache: false,
        async: false,
        success: function(response){
 
            var parsed = JSON.parse(response);
            
            if(parseInt(parsed.ind) == 1)
            {         
               
                $("#dialog-start-conversation").dialog("close");
                
                
                
            }
            else
            {                
                
}     
            $("body").css("cursor", "auto");
            
        }  
    }); // end AJAX upita 
     return false;  
}
function save_description(userId){
    var description = $("#description_"+userId).val();
    //var fromId=$("#from_id").val();
    var ajaxURL = CONTROLLER_URL + '/save_description/';
    $("body").css("cursor", "wait");
    // Ajax upit
    $.ajax({  
        type: "POST", 
        url: ajaxURL,
        data: "description="+description + "&fromId="+userId,
        cache: false,
        async: false,
        success: function(response){
 
            var parsed = JSON.parse(response);
            var html = "";
            if(parseInt(parsed.ind) == 1)
            {         
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-info ui-corner-all').show().html(html).delay(3000).hide(500); 
            }
            else
            {                
                html += "<p><span class='highlight-icon ui-icon ui-icon-info'></span>";      
                html += parsed.msg +"</p>";

                $('#response').show().addClass('ui-state-error ui-corner-all').html(html); 
            }     
            $("body").css("cursor", "auto");
            
        }  
    }); // end AJAX upita 
     return false;  
}